package cn.edu.swu.zwy.book;

import cn.edu.swu.zwy.db.DBTool;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

public class UpdateBookServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        int id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        Float price = Float.valueOf(request.getParameter("price"));
        String content = request.getParameter("content");

        Book book = new Book(id, name, author, price, content);

        try {
            BookRepo.getInstance().updateBook(book);
            response.sendRedirect("./main");
        } catch (SQLException | IOException e) {
            throw new ServletException(e);
        }
    }

}
